package com.dsg.service.contact.impl;

import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.dao.contact.DsgContactLogMapper;
import com.dsg.entity.contact.DsgContactLog;
import com.dsg.entity.contact.DsgContactLogSub;
import com.dsg.entity.dto.ContactLogDto;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.contact.IDsgContactLogService;
import com.dsg.service.contact.IDsgContactLogSubService;
import com.dsg.util.SnowflakeKeyWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * CONTACT LOG主表 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-10-06
 */
@Service
public class DsgContactLogServiceImpl extends DsgServiceImpl<DsgContactLogMapper, DsgContactLog> implements IDsgContactLogService {

    @Autowired
    private IDsgContactLogSubService dsgContactLogSubService;

    public List<ContactLogDto> getLogList(String userId) {
        return baseMapper.getLogList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWhole(DsgContactLog dsgContactLog) {
        if (dsgContactLog != null) {
            dsgContactLog.setOid(UUIDGenerator.getUUID());
            dsgContactLog.setCaseCode(SnowflakeKeyWorker.nextStrId());
            dsgContactLog.setLogTime(new Date());
            int i = baseMapper.insert(dsgContactLog);
            if (i > 0) {
                //操作子表
                return insertDsgContactLog(dsgContactLog);
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWhole(DsgContactLog dsgContactLog) {
        if (dsgContactLog != null) {
            int upInt = baseMapper.updateById(dsgContactLog);
            if (upInt > 0) {
                Map<String, Object> columnMap = new HashMap<>();
                columnMap.put("PID", dsgContactLog.getOid());
                dsgContactLogSubService.removeByMap(columnMap);
                //操作子表
                return insertDsgContactLog(dsgContactLog);
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 批量插入子表
     * @param dsgContactLog
     * @return
     */
    private boolean insertDsgContactLog(DsgContactLog dsgContactLog) {
        if (CollectionUtil.isNotEmpty(dsgContactLog.getDsgContactLogSubList())) {
            List<DsgContactLogSub> dsgContactLogSubList = dsgContactLog.getDsgContactLogSubList();
            dsgContactLogSubList.forEach(I -> {
                I.setOid(UUIDGenerator.getUUID());
                I.setPid(dsgContactLog.getOid());
            });
            dsgContactLogSubService.saveBatch(dsgContactLogSubList);
        }
        return true;
    }
}
